package com.smartrobot.robot;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.smartrobot.util.ConvUtil;


public abstract class AbstractRobot implements IRobot{
	protected  List<AbstractRobot> allRobots=new ArrayList<AbstractRobot>();
	
	protected List<AbstractRobot> getAllRobots(){
		if(allRobots.size()==0){
		String[] robots=ConvUtil.getSpringContext().getBeanNamesForType(AbstractRobot.class);
		for(String robot:robots){
			AbstractRobot irobot = (AbstractRobot) ConvUtil.getSpringContext().getBean(
					robot);
			allRobots.add(irobot);
		}
		}
		return allRobots;
	}

	public final boolean containFriend(String emailAddress){
		boolean isContain=false;
		if(this.localContainFriend(emailAddress)){
			isContain=true;
		}else{
			for(AbstractRobot childRobot:getAllRobots()){
				if(childRobot.localContainFriend(emailAddress)){
					isContain=true;
				}
			}
		}
		
		return isContain;
		
	}
	
	
	public final Map<String,String> getFriendList(){
		Map<String,String> allFriends=new HashMap<String,String>();
		for(AbstractRobot childRobot: getAllRobots()){
			allFriends.putAll(childRobot.localGetFriendList());
		}
		return allFriends;
	}
	
	public final String getOnlineStatus(String emailAddress){
		String status="ONLINE";
		for(AbstractRobot childRobot: getAllRobots()){
			if(childRobot.localContainFriend(emailAddress)){
				status= childRobot.localGetOnlineStatus(emailAddress);
				break;
			}
		}
		return status;
	}
	
	public final String getDisplayName(String emailAddress){
		String dname="unknown";
		for(AbstractRobot childRobot: getAllRobots()){
			if(childRobot.localContainFriend(emailAddress)){
				dname= childRobot.localGetDisplayName(emailAddress);
				break;
			}
		}
		return dname;
	}
	
	
	public abstract String localGetOnlineStatus(String emailAddress);
	public abstract Map<String,String> localGetFriendList();
	public abstract boolean localContainFriend(String emailAddress);
	public abstract String localGetDisplayName(String emailAddress);
}
